/**
 * Created by xiewl on 2016/4/13.
 */

//url参数配置
var urlParam = {
    baseApiUrl : "http://api.leley.org:8082/",
    // baseApiUrl : "http://172.16.0.167:8080/",
    basePictureUrl : "http://img.leley.org/",
    baseV1DuserUrl : "app/v1/duser",
    baseV2PuserUrl : "app/v2/puser",
    baseV2WfollowupUrl : "app/v2/wfollowup",
    ctype : 'pc',
    usertype : '1',
    clientversion :'2.0'
};

//http请求基础模块
function httpRequest(url,method,jsondata,callback){
    var urlto = urlParam.baseApiUrl + url;
    var timeStamp = $$.util.timeStamp();
    var sign = localStorage.sign || 'login';
    var token = localStorage.token || 'login';
    $.ajax({
        url : urlto,
        type:'post',
        data : {
            METHOD:method,
            PHONE:localStorage.phone,
            SIGN:sign,
            TOKEN:token,
            RECDATA:jsondata,
            REQTIME:timeStamp,
            CTYPE:urlParam.ctype,
            USERTYPE:urlParam.usertype,
            VERSION:urlParam.clientversion
        },
        complete: function (XMLHttpRequest, textStatus) {
            //token失效
            if(XMLHttpRequest.responseText && JSON.parse(XMLHttpRequest.responseText).code == "10007"){
                location.href = "../login/index.html?error=1";
                return false;
            }
            callback(XMLHttpRequest.responseText,textStatus);
        }
    });
}
//ajax请求类封装模块
function followupPlan(){
    var _this = this;
    //登录
    _this.login = function (phone,pwd,callback) {
        var method = "dlogin";
        localStorage.phone = phone;
        var password = hex_md5(pwd);
        var sdata = JSON.stringify({
            "uid":phone,
            "pwd":password,
            'ctype':urlParam.ctype,
            'cversion':urlParam.clientversion
        });
        httpRequest(urlParam.baseV1DuserUrl,method,sdata,callback);
    };
    //获取科室列表
    _this.getDeptList = function (callback) {
        var method = 'wdeptlist';
        var sdata = JSON.stringify({});
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //获取医院列表
    _this.getHospList = function (callback) {
        var method = 'whosplist';
        var sdata = JSON.stringify({});
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //关键词搜索医生列表
    _this.searchDoctorList = function (keyword,callback) {
        var method = 'doctorlist';
        var sdata = JSON.stringify({
            code : keyword,
            startpageno:0,
            num : 20
        });
        httpRequest(urlParam.baseV1DuserUrl,method,sdata,callback);
    };
    //获取随访模板列表
    _this.getFollowupTempList = function (page,pageSize,subdepttype,hospitalid,callback) {
        var method = 'wfollowuptemplatelistnew';
        var sdata = JSON.stringify({
            page:page,
            pagesize:pageSize,
            subdepttype:subdepttype,
            hospitalid:hospitalid
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //获取随访模板基本信息
    _this.getFollowupTempInfo = function (tempId,callback) {
        var method = 'wgettemplateinfo';
        var sdata = JSON.stringify({
            templateid : tempId
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //保存随访模板基本信息
    _this.setFollowupTempBase = function (templateid,doctorid,doctorusername,name,diseasename,callback) {
        var method = 'wcreatetemplate';
        var sdata = JSON.stringify({
            id:templateid,
            doctorid:doctorid,
            doctorusername:doctorusername,
            name:name,
            diseasename:diseasename
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //删除随访模板
    _this.delFollowupTemp = function (templateid,callback) {
        var method = 'wdeletetemplate';
        var sdata = JSON.stringify({
            templateid:templateid
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //获取模板节点列表
    _this.getTempNodeList = function (templateid,callback) {
        var method = 'wgettemplatesteps';
        var sdata = JSON.stringify({
            templateid:templateid
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //保存模板节点列表
    _this.setTempNodeList = function (stepArray,templateid, callback) {
        var method = 'wsavetemplatesteps';
        var sdata = JSON.stringify({
            steps: stepArray,
            templateid:templateid
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback)
    };
    //获取模板节点表单
    _this.getTempNodeForm = function (formid, callback) {
        var method = 'wgetform';
        var sdata = JSON.stringify({
            formid: formid
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //保存模板节点表单
    _this.setTempNodeForm = function (stepid,newone,formModel,callback) {
        var method = 'wsaveform';
        var sdata = JSON.stringify({
            stepid : stepid,
            newone : newone,
            form : formModel
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //删除模板节点表单
    _this.delTempNodeForm = function (stepid,formid,callback) {
        var method = 'wdeleteform';
        var sdata = JSON.stringify({
            stepid : stepid,
            formid : formid
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback);
    };
    //获取静态资源真实路径
    _this.getRealSrc = function (srcurl,callback) {
        var method = 'wrealfileurl';
        var sdata = JSON.stringify({
            srcurl : srcurl
        });
        httpRequest(urlParam.baseV2WfollowupUrl,method,sdata,callback)
    }
}
